package com.bao.auth.server.biz.service.auth.impl;

import com.bao.auth.server.biz.service.auth.ValidateCodeService;
import com.bao.common.constant.CacheKey;
import com.bao.core.exception.BizException;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.ChineseCaptcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.CacheObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

@Service("ValidateCodeServiceImpl")
public class ValidateCodeServiceImpl implements ValidateCodeService {
    
    //j2cache工具类，用来保存缓存
    @Autowired
    private CacheChannel cache;
    @Override
    public void create(String key, HttpServletResponse response) throws IOException {
        if(StringUtils.isBlank(key)) {
            throw BizException.validFail("验证码key不能为空");
        }
        //设置验证码以图片形式输出到前端
        setHeader(response,"png");

        Captcha captcha = new ArithmeticCaptcha(115,42);
        captcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
        //把验证码放入缓存
        cache.set(CacheKey.CAPTCHA, key, StringUtils.lowerCase(captcha.text()));
        //把验证码输出到前端
        captcha.out(response.getOutputStream());
    }

    @Override
    public boolean check(String key, String value) {
        if (StringUtils.isBlank(value)) {
            throw BizException.validFail("请输入验证码");
        }
        //从缓存中获取验证码
        CacheObject cacheObject = cache.get(CacheKey.CAPTCHA, key);
        if(cacheObject.getValue() == null){
            throw BizException.validFail("验证码已过期");
        }
        if (!StringUtils.equalsIgnoreCase(value, String.valueOf(cacheObject.getValue()))) {
            throw BizException.validFail("验证码不正确");
        }
        //验证通过，从缓存中删除验证码
        cache.evict(CacheKey.CAPTCHA, key);
        return true;
    }

    /**
     * 根据类型创建验证码
     * @param type 验证码类型，如：png、gif(传入的类型忽略大小写)
     * @return
     */
    private Captcha createCaptcha(String type){
        Captcha captcha = null;
        if (StringUtils.equalsIgnoreCase(type, "gif")) {
            captcha = new GifCaptcha(115 , 42, 4);
            captcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
        }else if (StringUtils.equalsIgnoreCase(type, "png")) {
            captcha = new SpecCaptcha(115 , 42, 4);
            captcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
        }else if (StringUtils.equalsIgnoreCase(type, "arithmetic")) {
            captcha = new ArithmeticCaptcha(115 , 42, 4);
            captcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
        }else {
            captcha = new ChineseCaptcha(115 , 42, 4);
            captcha.setCharType(Captcha.TYPE_DEFAULT);
        }
        return captcha;
    }

    /**
     * 根据图片类型、设置验证码相应类型
     * @param response 响应
     * @param type 验证码类型
     */
   private void setHeader(HttpServletResponse response, String type) {
        if (StringUtils.equalsIgnoreCase(type, "gif")) {
            response.setContentType(MediaType.IMAGE_GIF_VALUE);
        } else {
            response.setContentType(MediaType.IMAGE_PNG_VALUE);
        }
        response.setHeader(HttpHeaders.PRAGMA,"No-cache");
        response.setHeader(HttpHeaders.CACHE_CONTROL,"No-cache");
        response.setDateHeader(HttpHeaders.EXPIRES,0L);
   }
}
